import struct
import pickle
import traceback

CONFIG_VERSION = 4
CONFIG_PATH = "SoundsDeployer.config"

class Config:
    __slots__ = (
        "workspace",
        "xboxList",
    )

    def __init__(self):
        self.workspace = ""
        self.xboxList = []

    def save(self, path = CONFIG_PATH):
        try:
            data = pickle.dumps(self)
            with open(path, "wb") as f:
                f.write(struct.pack("<ii", CONFIG_VERSION, len(data)))
                f.write(data)
        except:
            traceback.print_stack()

def load(path = CONFIG_PATH):
    try:
        with open(path, "rb") as f:
            version = f.read(8)
            version, size = struct.unpack("<ii", version)
            if version != CONFIG_VERSION:
                return Config()

            data = f.read(size)
            return pickle.loads(data)
    except:
        return Config()
